// 监听类型选择点击事件
$('.sub-menu').click(function() {
	// body...
	var typeName = $(this).text();

	displayPage(1, typeName);

});

// 显示首页
function displayIndex() {
	var jo = {
		"page" : 1,
		"type" : "all"
	};

	$(document)
			.ready(
					function() {
						$
								.ajax({
									type : "post",
									url : "getBooks.action",
									dataType : "json",
									data : jo,
									success : function(data, textStatus) {
										var jsonarray = data.books;
										// 返回的是书籍信息
										var posters = document
												.getElementsByClassName('featured-post-slide');
										var articles = document
												.getElementsByTagName('article');
										// 滚动栏内书籍
										for (var i = 0; i < 3; i++) {
											posters[i]
													.getElementsByClassName('bookId')[0].innerText = jsonarray[i].bookid;
											posters[i]
													.getElementsByClassName('post-background')[0].style['background-image'] = "url("
													+ jsonarray[i].cover + ")";
											posters[i]
													.getElementsByClassName('sub-menu')[0].innerText = jsonarray[i].type;
											posters[i]
													.getElementsByTagName('a')[1].innerText = jsonarray[i].name;
										}

										// 非滚动栏内书籍

										for (var i = 0; i < articles.length; i++) {
											// articles[i].innerHTML=setArticle(jsonarray[i+3]);

											articles[i]
													.getElementsByClassName('bookId')[0].innerText = jsonarray[i + 3].bookid;

											articles[i]
													.getElementsByTagName('img')[0].src = jsonarray[i + 3].cover;

											articles[i]
													.getElementsByClassName('sub-menu')[0].innerText = jsonarray[i + 3].type;

											var star = '';
											for (var j = 0; j < jsonarray[i + 3].star; j++)
												star += "★";
											articles[i]
													.getElementsByClassName('rating')[0].innerHTML = star;

											articles[i]
													.getElementsByTagName('h1')[0].innerText = jsonarray[i + 3].name;

											articles[i]
													.getElementsByClassName('entry-excerpt')[0].innerText = jsonarray[i + 3].description;

											if (i % 3 == 0) {
												articles[i].style.left = '66.67%';
												var top = 0;
												for (var j = i; j > 0; j -= 3) {
													top += articles[j - 3].offsetHeight;
													top += 40;
												}

											} else if (i % 3 == 1) {
												articles[i].style.left = '0%';
												var top = 0;
												for (var j = i; j > 0; j -= 3) {
													if (j == 1) {
														top += posters[0].offsetHeight;
													} else {
														top += articles[j - 3].offsetHeight;
													}
													top += 40;
												}
											} else if (i % 3 == 2) {
												articles[i].style.left = '33.33%';
												var top = 0;
												for (var j = i; j > 0; j -= 3) {
													if (j == 2) {
														top += posters[0].offsetHeight;
													} else {
														top += articles[j - 3].offsetHeight;
													}
													top += 40;
												}
											}
											articles[i].style.top = top + 'px';
										}
									}
								});
					});

}

// 获取图书并显示
// 类型页面num根据page
function displayPage(num, typeName) {

	var jo = {
		"page" : num,
		"type" : typeName
	};

	// 获取跳转的url
	var cur_url = window.location.href;
	var urlTo;
	if (cur_url.indexOf("/jsp") >= 0) {
		urlTo = "category.jsp?type=" + encodeURI(typeName)
				+ encodeURI("&page=#") + num;
	} else {
		urlTo = "jsp/category.jsp?type=" + encodeURI(typeName)
				+ encodeURI("&page=#") + num;
	}

	window.location.href = urlTo;

	// 显示数据
	$(document).ready(
			function() {
				$.ajax({
					type : "post",
					url : "../getBooks.action",
					dataType : "json",
					data : jo,
					success : function(data) {
						// 返回的data为{books,jsonArray},一个名为books的jsonArray
						var jsonarray = data.books;

						var articles = document.getElementById('articles');// 获取文章显示框

						for (var i = 0; i < jsonarray.length; i++) {
							setArticle(articles,i, jsonarray[i].bookid,
									jsonarray[i].cover, jsonarray[i].type,
									jsonarray[i].star, jsonarray[i].name,
									jsonarray[i].description);

						}
						
						ssBricksAnimate();
						
						
					}

				});
			});

}

var ssBricksAnimate = function() {

	var animateEl = $('.animate-this');

	$(document).ready(function() {
		setTimeout(function() {
			animateEl.each(function(ctr) {
				var el = $(this);

				setTimeout(function() {
					el.addClass('animated fadeInUp');
				}, ctr * 200);

			});
		}, 200);
	});

	$(window).resize(function() {
		// remove animation classes
		animateEl.removeClass('animate-this animated fadeInUp');
	});

};

// 动态生成article
function setArticle(articles,i, bookid, cover, type, star, name, description) {
	var article = document.createElement('article');
	article.className = 'brick entry format-standard animate-this';
	
	// book id
	var bookId = document.createElement('div');
	bookId.className = 'bookId';
	bookId.innerText = bookid;
	bookId.style.display='none';

	var entryThumb = document.createElement('div');
	entryThumb.className = 'entry-thumb';
	var thumbLink = document.createElement('a');
	thumbLink.href = 'single-standard.html';
	thumbLink.className = 'thumb-link';
	// book cover
	var img = document.createElement('img');
	img.src = '../' + cover;

	thumbLink.appendChild(img);
	entryThumb.appendChild(thumbLink);

	var entryText = document.createElement('div');
	entryText.className = 'entry-text';
	var entryHeader = document.createElement('div');
	entryHeader.className = 'entry-header';
	var entryMeta = document.createElement('div');
	entryMeta.className = 'entry-meta';
	var catLinks = document.createElement('span');
	catLinks.className = 'cat-links';
	// book type
	var subMenu = document.createElement('a');
	subMenu.className = 'sub-menu';
	subMenu.href = 'javascript:void(0)';
	subMenu.innerText = type;
	// book star
	var rating = document.createElement('div');
	rating.className = 'rating';
	var starCount = '';
	for (var j = 0; j < star; j++)
		starCount += "★";
	rating.innerText = starCount;
	// book name
	var entryTitle = document.createElement('h1');
	entryTitle.className = 'entry-title';
	var title = document.createElement('a');
	title.href = 'single-standard.html';
	title.innerText = name;
	// book description
	var entryExcerpt = document.createElement('div');
	entryExcerpt.className = 'entry-excerpt';
	entryExcerpt.innerText = description;

	catLinks.appendChild(subMenu);
	entryMeta.appendChild(catLinks);
	entryMeta.appendChild(rating);
	entryTitle.appendChild(title);
	entryHeader.appendChild(entryMeta);
	entryHeader.appendChild(entryTitle);
	entryText.appendChild(entryHeader);
	entryText.appendChild(entryExcerpt);

	article.appendChild(bookId);
	article.appendChild(entryThumb);
	article.appendChild(entryText);

	articles.appendChild(article);
}

function searchResults(num) {
	var keyWord = getVarValue("s");
	var jo = {
		"keyword" : keyWord,
	};

	$(document)
			.ready(
					function() {
						$
								.ajax({
									type : "post",
									url : "../searchBook.action",
									dataType : "json",
									data : jo,
									success : function(data, textStatus) {
										// 返回的data为{books,jsonArray},一个名为books的jsonArray
										var jsonarray = data.books;
										// 返回的是书籍信息
										var articles = document
												.getElementsByTagName('article');
										// 返回的书籍数目不足页面应有的书籍板块数目
										if (jsonarray.length < articles.length) {
											// 类型页面Category
											// 没有该类别书籍
											if (jsonarray.length == 0) {
												document
														.getElementById('bricks').style.display = "none";

											} else {
												for (var i = 0; i < jsonarray.length; i++) {
													articles[i]
															.getElementsByClassName('bookId')[0].innerText = jsonarray[i].bookid;

													articles[i]
															.getElementsByTagName('img')[0].src = "../"
															+ jsonarray[i].cover;

													articles[i]
															.getElementsByClassName('sub-menu')[0].innerText = jsonarray[i].type;

													var star = '';
													for (var j = 0; j < jsonarray[i].star; j++)
														star += "★";
													articles[i]
															.getElementsByClassName('rating')[0].innerHTML = star;

													articles[i]
															.getElementsByTagName('h1')[0].innerText = jsonarray[i].name;

													articles[i]
															.getElementsByClassName('entry-excerpt')[0].innerText = jsonarray[i].description;

													var per = (i % 3) * 33.33
															+ "%";
													articles[i].style.left = per;
													var top = 0;
													for (var j = i; j >= 3; j -= 3) {
														top += articles[j - 3].offsetHeight;
														top += 40;
													}
													// top+=40;
													articles[i].style.top = top
															+ 'px';

												}
												for (var i = jsonarray.length; i < articles.length; i++) {
													articles[i].style.display = "none";
												}

											}
										} else {
											for (var i = 0; i < jsonarray.length; i++) {
												// alert(jsonarray[i].cover+jsonarray[i].type+jsonarray[i].name+jsonarray[i].description);
												articles[i]
														.getElementsByClassName('bookId')[0].innerText = jsonarray[i].bookid;

												articles[i]
														.getElementsByTagName('img')[0].src = "../"
														+ jsonarray[i].cover;

												articles[i]
														.getElementsByClassName('sub-menu')[0].innerText = jsonarray[i].type;

												var star = '';
												for (var j = 0; j < jsonarray[i].star; j++)
													star += "★";
												articles[i]
														.getElementsByClassName('rating')[0].innerHTML = star;

												articles[i]
														.getElementsByTagName('h1')[0].innerText = jsonarray[i].name;

												articles[i]
														.getElementsByClassName('entry-excerpt')[0].innerText = jsonarray[i].description;

												var per = (i % 3) * 33.33 + "%";
												articles[i].style.left = per;
												var top = 0;
												for (var j = i; j >= 3; j -= 3) {
													top += articles[j - 3].offsetHeight;
													top += 40;
												}
												// top+=40;
												articles[i].style.top = top
														+ 'px';

											}
										}

									}
								});
					});
}

function changePaginator(total_pages, cur_page) {
	$('#PageItems').jqPaginator('option', {
		totalPages : total_pages,
		currentPage : cur_page
	});
}

function getVarValue(variable) {
	var exp = window.location.href.split("?")[1];
	var vars = exp.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return decodeURI(pair[1]);
		}
	}
	return false;
}

if ($('#TypeOfThePage').length > 0) {
	// 从URL中解析获取Category类别
	document.getElementById('TypeOfThePage').innerText = "Category: "
			+ getVarValue("type");
}
